<template>
	<view class="content">
		<view>
			<view class="order_container_top border_bottom display align-items space-between back">
				<view class="order_container_top_name " :class="{act : current==0}" @click="onClickItem(0)">全部</view>
				<view class="order_container_top_name " :class="{act : current==1}" @click="onClickItem(1)">待付款</view>
				<view class="order_container_top_name " :class="{act : current==2}" @click="onClickItem(2)">待发货</view>
				<view class="order_container_top_name " :class="{act : current==3}" @click="onClickItem(3)">待收货</view>
				<view class="order_container_top_name " :class="{act : current==4}" @click="onClickItem(4)">已完成</view>
			</view>
			<view class="form_list">
				<view class="list" v-for="(item,index) in orderList" :key="index">
					<view class="list_w">
						<view class="tit" @click="goDetails">
							<view class="right" v-if="item.status==1">待付款</view>
							<view class="right" v-if="item.status==2">待发货</view>
							<view class="right" v-if="item.status==3">待收货</view>
							<view class="right" v-if="item.status==4">已完成</view>
						</view>
						<view class="con" @click="goDetails(item.id)" v-for="(i,idx) in item.goods" :key="idx">
							<image :src="imgUrl+i.goods_image" mode=""></image>
							<view class="left">
								<view class="name ellipsis_more ellipsis_more_2">{{i.goods_name}}</view>
								<view class="num">×{{i.goods_num}}</view>
								<view class="mony">¥{{i.goods_price}}</view>
							</view>
						</view>
						<view class="heji">
							共{{item.goods_num}}件商品 合计：<text>¥{{item.goods_amount}}</text>
						</view>
						<view class="bot" v-if="item.status==1">
							<!-- 待付款 -->
							<view class="zhifu" v-if="item.status==1" @click="goPay(item.id)">立即支付</view>
							<view class="quxiao" v-if="item.status==1" @click="cancleOrder(item.id)">取消订单</view>
						</view>
						<view class="bot" v-if="item.status==3">
							<view class="zhifu" @click="goLogistics(item.id)" v-if="item.status==3">物流信息</view>
							<!-- 待收货 -->
							<view class="zhifu" v-if="item.status==3" @click="orderReceive(item.id)">确认收货</view>
						</view>
					</view>
				</view>
			</view>
		</view>
		<view class="no_thing" v-if="noOrder">
			<image src="../../../static/icon/no_order.png"></image>
			<view class="no_thing_text">暂无内容</view>
		</view>
		<view class="over" v-if="flag">我也是有底线的</view>
		<is-login v-on:childClick='listenToClose' v-on:childClickAgain='listenToCloseAgain' v-if="isLogin==0"></is-login>
	</view>
</template>

<script>
	import isLogin from '../../../components/isLogin.vue'
	import {
		uniSegmentedControl
	} from '@dcloudio/uni-ui'
	export default {
		components: {
			uniSegmentedControl,
			isLogin
		},
		data() {
			return {
				current: 0,
				orderList: [],
				page: 1,
				limit: 10,
				flag: false,
				imgUrl: '',
				noOrder: false,
				isLogin: 1
			}
		},
		onLoad(optain) {
			let that = this
			this.imgUrl = that.$imgUrl
			if (!uni.getStorageSync('openid')) {
				this.isLogin = 0
			} else {
				console.log(optain.status)
				this.current = optain.status
				this.getOrderList()
			}
		},
		onShow() {
			let that = this;
			that.flag = false;
			that.noOrder = false;
			that.orderList = [];
			if (!uni.getStorageSync('openid')) {
				this.isLogin = 0
			} else {
				this.getOrderList()
			}
		},
		onPullDownRefresh() {
			uni.showToast({
				icon: 'loading',
				title: '加载中',
			});
			this.page = 1
			this.orderList = []
			this.flag = false
			this.noOrder = false

			setTimeout(() => {
				this.getOrderList(() => {
					uni.hideLoading()
					uni.stopPullDownRefresh()
				})
			}, 1000)
		},
		onReachBottom() {
			if (this.orderList.length < this.page * this.limit) {
				return this.flag = true
			}
			this.noOrder = false
			this.page++
			this.getOrderList()
		},
		methods: {
			// 分类 
			onClickItem(e) {
				this.current = e
				this.page = 1
				this.orderList = []
				this.flag = false
				this.noOrder = false
				this.getOrderList()
			},
			// 获取订单列表
			getOrderList(callback) {
				let that = this
				console.log(that.current)
				var data = {
					openid: uni.getStorageSync('openid'),
					status: that.current,
					page: that.page,
					limit: that.limit
				}
				this.$myRequest.post(this.$api.order.getOrderList, data).then(res => {
					if (res.code == '200') {
						callback && callback()
						if (that.page == 1) {
							if (res.data.data.length == 0) {
								this.noOrder = true
								this.flag = false
								return false;
							}
							this.orderList = res.data.data
						} else {
							this.orderList = [...this.orderList, ...res.data.data]
						}
					} else {
						uni.showToast({
							title: res.msg,
							icon: "none"
						})
					}
				})
			},
			// 立即支付
			goPay(id) {
				let that = this
				var orderId = id
				uni.navigateTo({
					url: "../../shopCar/confirmOrder/index?orderId=" + orderId
				})
			},
			// 取消订单
			cancleOrder(id) {
				let that = this
				var data = {
					openid: uni.getStorageSync('openid'),
					id: id
				}
				uni.showModal({
					title: '提示',
					content: '是否取消该订单',
					success: function(res) {
						if (res.confirm) {
							that.$myRequest.post(that.$api.order.orderCancel, data).then(res => {
								if (res.code == '200') {
									uni.showToast({
										title: '取消成功',
										icon: "success",
										success() {
											that.orderList = []
											that.getOrderList()
										}
									})
								} else {
									uni.showToast({
										title: res.msg,
										icon: "none"
									})
								}
							})
						} else if (res.cancel) {

						}
					}
				});
			},
			// 确认收货
			orderReceive(id) {
				let that = this
				var data = {
					openid: uni.getStorageSync('openid'),
					id: id
				}
				uni.showModal({
					title: '提示',
					content: '是否确认收货',
					success: function(res) {
						if (res.confirm) {
							that.$myRequest.post(that.$api.order.orderReceive, data).then(res => {
								if (res.code == '200') {
									uni.showToast({
										title: '收货成功',
										icon: "success",
										success() {
											setTimeout(function() {
												that.current = '4'
												that.getOrderList()
											}, 1500)

										}
									})
								} else {
									uni.showToast({
										title: res.msg,
										icon: "none"
									})
								}
							})
						} else if (res.cancel) {

						}
					}
				});

			},
			// 查看物流
			goLogistics(id) {
				uni.navigateTo({
					url: "./logistics?id=" + id
				})
			},
			// 查看订单详情
			goDetails(id) {
				uni.navigateTo({
					url: "orderDetails?id=" + id
				})
			},
			// 监听
			listenToClose(msg) {
				this.isLogin = msg
			},
			listenToCloseAgain() {
				this.getOrderList()
			},
		}
	}
</script>

<style scoped lang="scss">
	.act {
		border-bottom: 2rpx solid #294D7C !important;
	}

	.content {
		width: 690rpx;
		margin: 0 auto;

		.order_container_top {
			height: 80rpx;
			width: 690rpx;
			position: -webkit-sticky;
			position: sticky;
			top: 0;
			left: 0;
			right: 0;
			margin: auto;

			.order_container_top_name {
				padding: 0 8rpx;
				height: 78rpx;
				line-height: 78rpx;
			}

		}

		.form_list {

			.list {
				width: 690rpx;
				// height: 262rpx;
				border: 1px solid #E3E3EF;
				border-radius: 10rpx;
				margin: 30rpx auto;

				.list_w {
					width: 650rpx;
					margin: 0 auto;

					.tit {
						width: 100%;
						height: 66rpx;
						line-height: 66rpx;

						.left {
							font-size: 22rpx;
							color: #333333;
							float: left;
						}

						.cen {
							font-size: 20rpx;
							color: #666666;
							float: left;
						}

						.right {
							color: #F21515;
							font-size: 26rpx;
							float: right;
						}
					}

					.con {
						display: flex;
						align-items: center;
						height: 140rpx;
						margin-bottom: 25rpx;

						image {
							width: 140rpx;
							height: 140rpx;
							margin-right: 36rpx;
						}

						.left {
							width: 450rpx;

							.name {
								color: #333333;
								font-size: 26rpx;
								height: 70rpx;
							}

							.num {
								text-align: right;
								font-size: 22rpx;
								color: #666666;
							}

							.mony {
								font-size: 24rpx;
								color: #333333;
								text-align: right;
							}


						}
					}

					.heji {
						font-size: 28rpx;
						color: #333333;
						text-align: right;
						font-weight: bold;
						margin-bottom: 20rpx;

						text {
							color: #F21515;
						}
					}

					.bot {
						height: 78rpx;
						border-top: 1rpx solid #E3E3EF;

						.quxiao {
							float: right;
							text-align: center;
							line-height: 44rpx;
							color: #a1a1a7;
							width: 126rpx;
							height: 44rpx;
							border: 1px solid #E3E3EF;
							border-radius: 4rpx;
							margin-left: 20rpx;
							font-size: 22rpx;
							margin-top: 18rpx;
						}

						.zhifu {
							float: right;
							font-size: 22rpx;
							margin-top: 18rpx;
							text-align: center;
							line-height: 44rpx;
							color: #FFFFFF;
							background-color: #4b968d;
							width: 126rpx;
							height: 44rpx;
							// border: 2rpx solid #E3E3EF;
							border-radius: 4rpx;
							margin-left: 20rpx;
						}
					}
				}
			}
		}
	}
</style>
